package org.example.queueadmin.log;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/logs")
@RequiredArgsConstructor
public class MessageLogController {
    
    private final MessageLogService logService;
    
    @GetMapping("/{taskId}")
    public Page<MessageLog> getLogs(@PathVariable Long taskId, 
                                   @RequestParam(defaultValue = "0") int page,
                                   @RequestParam(defaultValue = "20") int size) {
        return logService.getLogs(taskId, page, size);
    }
    
    @GetMapping("/{taskId}/errors")
    public Page<MessageLog> getErrorLogs(@PathVariable Long taskId,
                                        @RequestParam(defaultValue = "0") int page,
                                        @RequestParam(defaultValue = "20") int size) {
        return logService.getErrorLogs(taskId, page, size);
    }
    
    @GetMapping("/{taskId}/stats")
    public LogStats getStats(@PathVariable Long taskId) {
        return new LogStats(
            logService.getSuccessCount(taskId),
            logService.getFailureCount(taskId)
        );
    }

    @GetMapping("/message/{id}")
    public ResponseEntity<MessageDetail> getMessageDetail(@PathVariable Long id) {
        return logService.getMessageDetail(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    public record LogStats(long successCount, long failureCount) {}

    public record MessageDetail(
        Long id,
        String timestamp,
        String direction,
        String source,
        String target,
        Boolean success,
        String errorMessage,
        String fullMessageContent
    ) {}
}
